package pl.diagnoser.client.tools;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.layout.SectionStackSection;

public class DiagnoserProperties{
	
	private static DataGetterServiceAsync dataGetterAsc = GWT.create(DataGetterService.class);
	
	public void getWidgetTitle(String widgetName, final Widget widget) {
		if (dataGetterAsc == null) {
			dataGetterAsc = GWT.create(DataGetterService.class);
		}
		
		AsyncCallback<String> callback = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				System.out.println(caught.getMessage());
			}

			public void onSuccess(String result) {
				widget.setTitle(result);
			}
		};
		dataGetterAsc.getWidgetTitle(widgetName, callback);
	}
	
	public void getLabelTitle(String widgetName, final Label label) {
		if (dataGetterAsc == null) {
			dataGetterAsc = GWT.create(DataGetterService.class);
		}
		
		AsyncCallback<String> callback = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				System.out.println(caught.getMessage());
			}

			public void onSuccess(String result) {
				label.setContents(result);
			}
		};
		dataGetterAsc.getWidgetTitle(widgetName, callback);
	}
	
	public void getFormItemTitle(String widgetName, final FormItem formItem) {
		if (dataGetterAsc == null) {
			dataGetterAsc = GWT.create(DataGetterService.class);
		}
		
		AsyncCallback<String> callback = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				System.out.println(caught.getMessage());
			}

			public void onSuccess(String result) {
				formItem.setTitle(result);
				formItem.redraw();
			}
		};
		dataGetterAsc.getWidgetTitle(widgetName, callback);
	}
	
	public void getSectionStackTitle(String widgetName, final SectionStackSection sectionStack) {
		if (dataGetterAsc == null) {
			dataGetterAsc = GWT.create(DataGetterService.class);
		}
		
		AsyncCallback<String> callback = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				System.out.println(caught.getMessage());
			}

			public void onSuccess(String result) {
				sectionStack.setTitle(result);
			}
		};
		dataGetterAsc.getWidgetTitle(widgetName, callback);
	}
	
	public void getListGridTitle(final String widgetName, final String listGridName, final ListGrid listGrid, final boolean refresh) {
		if (dataGetterAsc == null) {
			dataGetterAsc = GWT.create(DataGetterService.class);
		}
		
		AsyncCallback<String> callback = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				System.out.println(caught.getMessage());
			}

			public void onSuccess(String result) {
				listGrid.getField(widgetName).setTitle(result);
				if(refresh)
					listGrid.refreshFields();
			}
		};
		
		dataGetterAsc.getWidgetTitle(listGridName + "." + widgetName, callback);
	}
}
